//-------------------------------------------------------------------
//	Copyright (c) 2012-2014 Zhirnov Andrey
//	This file is part of the "UXGEN-ES" project.
//	See copyright notice in "Engine.h".
//-------------------------------------------------------------------

#pragma once

#include "Graphics/MetaData/Meta.Program.h"
#include "Graphics/MetaData/Meta.Font.h"
#include "Graphics/MetaData/Meta.Sprite.h"

#ifdef ENGINE_META_DATA
#	include "Graphics/Resources/Program.h"
#	include "Graphics/Resources/BitmapFont.h"
#	include "Graphics/Resources/Material.h"
#	include "Graphics/Resources/Texture.h"
#endif



	//
	// Bitmap Font
	//
#	define META_OBJECT			BitmapFontCfg
#	define META_OBJECT_NAME		TOSTRING( BitmapFont )
#	define META_NAME			MetaBitmapFont

#	define META_FIELDS( _decl ) \
		_decl( string,		address,	, ) \
		_decl( uint,		size,		, 32 ) \
		_decl( bool,		mipmaps,	, false )
			
#	include "Base/MetaData/MetaDataObject.h"



	namespace MetaData
	{

		struct ExtStringCfg
		{
			string	str;
			///
			ExtStringCfg () {}
			ExtStringCfg (uni_c_string s) : str(s) {}
		};


		template <>
		inline bool MetaDataUtils::Save<> (const WFilePtr &file, const ExtStringCfg &value)
		{
			FILE_RW_BEGIN();

			ext_string			estr;
			ext_string::array_t	arr;
			
			estr.Parse( (uni_c_string) value.str );
			estr.SwapMem( arr );
			
			FILE_RW_CHECK( MetaDataUtils::Save( file, arr ) );

			FILE_RW_RETURN();
		}

		template <>
		inline bool MetaDataUtils::Load<> (const RFilePtr &file, ExtStringCfg &value)
		{
			FILE_RW_BEGIN();

			ext_string::array_t	arr;

			FILE_RW_CHECK( MetaDataUtils::Load( file, arr ) );

			MemMover::Swap( value.str, arr );
			
			FILE_RW_RETURN();
		}
		

#	ifdef EXTERNAL_META_DATA
		
		inline UX_DECLARE_META_TYPEDEF( ExtStringCfg, string );

#	endif


#	ifdef ENGINE_META_DATA
		
		template <>
		inline void MetaDataUtils::Copy<> (TexturePtr &left, string &right, const EngineSubSystemsRef ess)
		{
			if ( right.Empty() )
				return;

			CHECK_ERR( ess->GetResourceManager()->Load( EResource::TEXTURE, right, left, Texture::GetDefaultLoadParams() ), void() );
		}

		template <>
		inline void MetaDataUtils::Copy<> (BitmapFontPtr &left, BitmapFontCfg &right, const EngineSubSystemsRef ess)
		{
			BitmapFont::BitmapFontLoadParams	load_params;

			load_params.height  = right.size;
			load_params.mipmaps = right.mipmaps;

			CHECK_ERR( ess->GetResourceManager()->Load( EResource::BITMAP_FONT, right.address, left, load_params ), void() );
		}
		
		template <>
		inline void MetaDataUtils::Copy<> (ext_string &left, ExtStringCfg &right, const EngineSubSystemsRef ess)
		{
			left.SetMem( right.str );
		}
		
		template <>
		inline void MetaDataUtils::Copy<> (array<ext_string> &left, array<ExtStringCfg> &right, const EngineSubSystemsRef ess)
		{
			left.Resize( right.Count(), false );

			FOR( i, left )
			{
				left[i].SetMem( right[i].str );
			}
			right.Clear();
		}

#	endif

	}	// MetaData
